import { getGrade, getPromotionStatus, ProjectFocus } from '@/utils/enum'

export interface ProjectFromBackend {
  id: number
  creatorOpenid: string
  projectName: string
  projectInfo: string
  direction: number // Maps to ProjectFocus enum
  memberCount: number
  crossSchool: number // 1 for true, 0 for false
  educationRequirement: number // Maps to educationRequirement enum
  skillsSummary?: string | null // Optional field, can be null
  skillDetails?: string // Optional field, can be null
  createdAt: string // Date in 'YYYY-MM-DD HH:mm:ss' format
  updatedAt: string // Date in 'YYYY-MM-DD HH:mm:ss' format
  emailPromotion: boolean // Indicates if email promotion is enabled
  lookcount: number // Number of views
  applicationCount: number // Number of applications received
  study: boolean // Indicates if positive students are allowed
  emailPromotionStatus: number // Maps to ProjectDelectStatus enum
  nickname?: string
  email?: string
  phone?: string
  // 添加审核相关字段
  auditStatus?: boolean | null // 项目审核状态: null-未审核, true-通过, false-不通过
  remark?: string | null // 审核备注信息
  initiatingSchool: string // 发起学校
}

export interface Project {
  id: number
  name: string
  description: string
  createdAt: string
  promotionStatusLabel: string
  promotionStatus: number // Maps to emailPromotionStatus
  promotionEnabled: boolean
  marked: boolean
  isCrossSchool: boolean
  teamSize: number
  initiatingSchool: string
  schoolRequirements: string
  projectFocus: string
  detailedRequirements: string
  allowPositiveStudents: boolean
  email: string
  phone: string
  applicantCount?: number // Optional field for application count
  viewCount?: number // Optional field for view count
  // 添加审核相关字段
  auditStatus?: boolean | null // 项目审核状态: null-未审核, true-通过, false-不通过
  auditRemark?: string | null // 审核备注信息
}

export function mapProjectFromBackend(element: ProjectFromBackend): Project {
  return {
    id: element.id,
    name: element.projectName,
    description: element.projectInfo,
    createdAt: element.createdAt.split(' ')[0], // Extract date part (e.g., '2025-07-30')
    promotionStatusLabel: element.emailPromotion
      ? getPromotionStatus(element.emailPromotionStatus)
      : '无需推广', // Maps to emailPromotionStatus
    promotionStatus: element.emailPromotionStatus, // Maps to emailPromotionStatus
    promotionEnabled: element.emailPromotion, // Maps to emailPromotion
    marked: false,
    isCrossSchool: element.crossSchool === 1, // Convert 1/0 to boolean
    teamSize: element.memberCount,
    initiatingSchool: element.initiatingSchool,
    schoolRequirements: getGrade(element.educationRequirement) || '无特殊要求', // Maps to educationRequirement
    projectFocus: ProjectFocus[element.direction] || '未知', // Maps to direction
    detailedRequirements: element.skillDetails || '无详细要求', // Use skillDetails or default
    allowPositiveStudents: element.study, // Maps to study
    email: element.email,
    phone: element.phone,
    // 映射审核相关字段
    auditStatus: element.auditStatus,
    auditRemark: element.remark,
  }
}

export function getPromotingBtnColor(status: number, enabled: boolean): string {
  switch (status) {
    case 0: // 未推广
      return enabled ? 'primary' : 'default'
    case 1: // 已推广
      return 'success'
    case 2: // 推广中
      return 'warning'
    case -1: // 推广失败
      return 'danger'
    default:
      return 'default'
  }
}
